package com.db.springcloud.controller;


import com.db.springcloud.pojo.User;
import com.db.springcloud.service.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class UserConsumerController {

    //消费者不应有service层
    //RestTemplate直接调用，注册到spring中
    //(url,实体:map,Class<T>ResponseType)
 //   @Autowired
//    private RestTemplate restTemplate;//提供多种便捷访问远程http服务的方法，简单的restful服务模板

    @Autowired
    private UserClientService userClientService;

    //ribbon这里的地址应该是一个变量
//    private static final String REST_URL_PREFIX = "http://localhost:8001";
//    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-USER";

    @RequestMapping("/consumer/user/add")
    public boolean addUser(User user){
        return userClientService.addUser(user);
//        return restTemplate.postForObject(REST_URL_PREFIX + "/user/add","",Boolean.class,user);
    }

    @RequestMapping("/consumer/user/get/{id}")
    public User queryById(@PathVariable("id") int id){
        return userClientService.queryById(id);
 //       return restTemplate.getForObject(REST_URL_PREFIX+"/user/get/"+id,User.class,id);
    }

    @RequestMapping("/consumer/user/getall")
    public List<User> queryAll(){
        return userClientService.queryAll();
        //      return restTemplate.getForObject(REST_URL_PREFIX+"/user/getall",List.class);
    }

//    @RequestMapping("/consumer/user/getbyid")
//    public User getById(int id){
//        return restTemplate.getForObject(REST_URL_PREFIX+"/user/getbyid?id={1}",User.class,id);
//    }

}
